#include "CommunicationSetView.h"
#include "ui_CommunicationSetForm.h"

CommunicationSetView::CommunicationSetView(QWidget *parent) :
    QWidget(parent), ui(new Ui::CommunicationSet)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::FramelessWindowHint);
    InitUi();

    ipaddrWgt = new IpAddressWgt(this);
    maskWgt = new IpAddressWgt(this);
    gatewayWgt = new IpAddressWgt(this);
    lisipaddrWgt = new IpAddressWgt(this);

    ui->hLayout_ipaddr->addWidget(ipaddrWgt);
    ui->hLayout_mask->addWidget(maskWgt);
    ui->hLayout_gateway->addWidget(gatewayWgt);
    ui->hLayout_lisipaddr->addWidget(lisipaddrWgt);

    m_settingHelper = SettingHelper::getSettingHelper();
    readSetting();

    connect(ui->checkBox_ack,SIGNAL(clicked(bool)),this,SLOT(slot_ackChecked(bool)));
}

CommunicationSetView::~CommunicationSetView()
{
    delete ui;
}

void CommunicationSetView::closeEvent(QCloseEvent *e)
{
    Q_UNUSED(e);
    writeSetting();
}

void CommunicationSetView::InitUi()
{
    if(ui->checkBox_ack->isChecked())
    {
        ui->nu_lineEdit_sec->setEnabled(true);
        ui->checkBox1->setEnabled(true);
    }
    else
    {
        ui->nu_lineEdit_sec->setEnabled(false);
        ui->checkBox1->setEnabled(false);
    }
}

void CommunicationSetView::readSetting()
{
    m_settingHelper->commuReadSetting();

    ipaddrWgt->setIpAddressString(m_settingHelper->m_CommuSet.ipaddr);
    maskWgt->setIpAddressString(m_settingHelper->m_CommuSet.mask);
    gatewayWgt->setIpAddressString(m_settingHelper->m_CommuSet.gateway);
    ui->lineEdit_mac->setText(m_settingHelper->m_CommuSet.mac);
   // ui->comboBox_protocol->setCurrentText(m_settingHelper->m_CommuSet.protocol);
    lisipaddrWgt->setIpAddressString(m_settingHelper->m_CommuSet.lisipaddr);
    ui->nu_lineEdit_IPPort->setText(m_settingHelper->m_CommuSet.IPPort);
    ui->checkBox_ack->setChecked(m_settingHelper->m_CommuSet.ack);
    ui->nu_lineEdit_sec->setText(QString::number(m_settingHelper->m_CommuSet.ackSec));
    ui->checkBox1->setChecked(m_settingHelper->m_CommuSet.continueTrans);
    ui->checkBox2->setChecked(m_settingHelper->m_CommuSet.communicat);
    ui->checkBox3->setChecked(m_settingHelper->m_CommuSet.getSampel);
    ui->checkBox4->setChecked(m_settingHelper->m_CommuSet.bitTran);
   // ui->comboBox_histogram->setCurrentText(m_settingHelper->m_CommuSet.histogram);
    //ui->comboBox_scatter->setCurrentText(m_settingHelper->m_CommuSet.scatter);
}

void CommunicationSetView::writeSetting()
{
    m_settingHelper->m_CommuSet.ipaddr = ipaddrWgt->getIpAddressString();
    m_settingHelper->m_CommuSet.mask = maskWgt->getIpAddressString();
    m_settingHelper->m_CommuSet.gateway = gatewayWgt->getIpAddressString();
    m_settingHelper->m_CommuSet.mac = ui->lineEdit_mac->text();
    m_settingHelper->m_CommuSet.protocol = ui->comboBox_protocol->currentText();
    m_settingHelper->m_CommuSet.lisipaddr = lisipaddrWgt->getIpAddressString();
    m_settingHelper->m_CommuSet.IPPort = ui->nu_lineEdit_IPPort->text();
    m_settingHelper->m_CommuSet.ack = ui->checkBox_ack->isChecked();
    m_settingHelper->m_CommuSet.ackSec = ui->nu_lineEdit_sec->text().toInt();
    m_settingHelper->m_CommuSet.continueTrans = ui->checkBox1->isChecked();
    m_settingHelper->m_CommuSet.communicat = ui->checkBox2->isChecked();
    m_settingHelper->m_CommuSet.getSampel = ui->checkBox3->isChecked();
    m_settingHelper->m_CommuSet.bitTran = ui->checkBox4->isChecked();
    m_settingHelper->m_CommuSet.histogram = ui->comboBox_histogram->currentText();
    m_settingHelper->m_CommuSet.scatter = ui->comboBox_scatter->currentText();

    m_settingHelper->commuWriteSetting();//将设置内容写入配置文件
}

void CommunicationSetView::slot_ackChecked(bool flag)
{
    if(flag)
    {
        ui->nu_lineEdit_sec->setEnabled(true);
        ui->checkBox1->setEnabled(true);
    }
    else
    {
        ui->nu_lineEdit_sec->setEnabled(false);
        ui->checkBox1->setEnabled(false);
    }
}
